package com.zyb.vcode.controller;

import com.zyb.constans.BaseConstants;
import com.zyb.utils.AjaxResult;
import com.zyb.utils.SendMsgUtils;
import com.zyb.utils.StrUtils;
import com.zyb.utils.VerifyCodeUtils;
import com.zyb.vcode.controller.domain.VCode;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.mail.SendFailedException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/vcode")
@Api(value = "验证码的API",description="验证码的CRUD功能")
public class ImageVerifyCodeController {
    @Value("${msg_Uid}")
    private String msg_Uid;
    @Value("${msg_key}")
    private String msg_key;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/imgCode/{key}")
    public AjaxResult getVCode(@PathVariable String key){
        try {
            //单体项目验证码实现-响应流
            String code = StrUtils.getRandomString(6).toLowerCase();
            redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
            String base64Img = VerifyCodeUtils.VerifyCode(110, 40, code);
            return AjaxResult.success().setData(base64Img);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("图片验证码生成失败");
            return AjaxResult.error().setMsg("网络异常,请联系管理员");
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult getSendSmsCode(@RequestBody VCode vCode){
        String phone = vCode.getPhone();
        String imageCode = vCode.getImageCode();
        String imageCodeKey = vCode.getImageCodeKey();
        if(phone == null || "".equals(phone.trim())){
            return AjaxResult.error().setMsg("手机号不能为空！");
        }
        if(imageCode == null || "".equals(imageCode.trim())){
            return AjaxResult.error().setMsg("验证码不能为空！");
        }
        if(imageCodeKey == null || "".equals(imageCodeKey.trim())){
            return AjaxResult.error().setMsg("非法访问！");
        }
        //校验叫验证码
        Object value = redisTemplate.opsForValue().get(imageCodeKey);
        if(value ==null){
            return AjaxResult.error().setMsg("图片验证码已失效,请重新获取");
        }
        if(!value.toString().equals(imageCode)){
            return AjaxResult.error().setMsg("验证码错误,请重新输入");
        }
        //手机验证
        // todo
        String phoneCode = StrUtils.getRandomString(6).toLowerCase();
        //防止重复发送验证码
        String keyTime = phone + BaseConstants.VCode.REGISTER_TIME;//获取上一次的时间
        //获取时间戳
        Object ObjectTime = redisTemplate.opsForValue().get(keyTime);
        if(ObjectTime == null){
            //获取当前时间
            long startTime = System.currentTimeMillis();
            //添加业务键标识
            String key = phone + BaseConstants.VCode.REGISTER;
            redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.VCode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
            redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.VCode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
        }else {
            //转换
            Long theTime = Long.valueOf(ObjectTime.toString());
            //结束时间
            long endTime = System.currentTimeMillis();
            if(endTime-theTime < 60000){
                return AjaxResult.error().setMsg("请勿重复获取验证码");
            }else {
                String key = phone + BaseConstants.VCode.REGISTER;
                phoneCode = (String)redisTemplate.opsForValue().get(key);
                //获取当前时间
                long startTime = System.currentTimeMillis();
                //添加业务键标识
                redisTemplate.opsForValue().set(key,phoneCode,BaseConstants.VCode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
                redisTemplate.opsForValue().set(keyTime,startTime,BaseConstants.VCode.REGISTER_TIME_OUT,TimeUnit.MINUTES);
            }
        }

        //发送短信验证码
        //SendMsgUtils.sendMsg(msg_Uid,msg_key,phone,phoneCode);
        System.err.println(phoneCode);
        return AjaxResult.success().setMsg("短信验证码成功");
    }
}
